package org.meteorshower.config;

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class AdminAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

    public AdminAuthenticationFilter(String AdminUrl) {
        super(AdminUrl);
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws AuthenticationException, IOException, ServletException {
        String username = httpServletRequest.getHeader("username");
        String password = httpServletRequest.getHeader("password");

        AdminAuthenticationToken token = new AdminAuthenticationToken(username,password);

        Authentication authentication = getAuthenticationManager().authenticate(token);
        return authentication;
    }

    @Override
    public void setAuthenticationManager(AuthenticationManager authenticationManager) {
        super.setAuthenticationManager(authenticationManager);
    }
}
